package Thread;

class Test {
    public int count = 0;

    public void add() {
        synchronized(this) {
            count++;
        }
    }

    /*synchronized public void add() {
        count++;
    }*/
}

public class ThreadDemo13 {
    public static void main(String[] args) throws InterruptedException {
        Test t = new Test();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 50_0000; i++) {
                t.add();
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 50_0000; i++) {
                t.add();
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("count = " + t.count);
    }
}
